<?php
namespace duoge\dingtalk;

use Yii;

class DingTalkAccessToken {

    public static function GET_CORP_ID() {
        $dingtalkconfig = require Yii::getAlias('@app/config/dingtalk.php');
        return $dingtalkconfig["corpId"];
    }

    public static function GET() {
        $dingtalkconfig = require Yii::getAlias('@app/config/dingtalk.php');
        $cache = Yii::$app->cache;
        $keyname = 'dingtalk_access_token_'.$dingtalkconfig['AppKey'];
        if($cache->exists($keyname)) {
            $json_txt = $cache->get($keyname);
        } else {
            $dingtalkclient = new DingTalkClient();
            $json_txt = json_decode($dingtalkclient->curl_get("https://oapi.dingtalk.com/gettoken?",[
                'appkey'=>$dingtalkconfig['AppKey'],
                'appsecret'=>$dingtalkconfig['AppSecret']
            ]));
            if($json_txt->errcode == 0) {
                $cache->add($keyname,$json_txt,$json_txt->expires_in);
            }
        }

        return $json_txt;
    }


}